package com.example.pagingdemo.paging.user;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.ViewModel;
import androidx.paging.DataSource;
import androidx.paging.LivePagedListBuilder;
import androidx.paging.PageKeyedDataSource;
import androidx.paging.PagedList;

import com.example.pagingdemo.api.Api;
import com.example.pagingdemo.api.RetrofitClient;
import com.example.pagingdemo.model.User;
import com.example.pagingdemo.model.UserResponse;
import com.example.pagingdemo.paging.AbsViewModel;
import com.example.pagingdemo.paging.PageConfig;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author: tuyrt7 on 2020/9/9.
 */
public class UserViewModel extends AbsViewModel<User> {

    @Override
    protected DataSource createDataSource() {
        return new UserDataSource();
    }

    public class UserDataSource extends PageKeyedDataSource<Integer, User> {

        public static final String SITE = "stackoverflow";

        @Override
        public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, User> callback) {
            int FIRST_PAGE = 1;
            RetrofitClient.get().buildApi(Api.BASE_URL2)
                    .getUsers(FIRST_PAGE, PageConfig.PER_PAGE_SIZE, SITE)
                    .enqueue(new Callback<UserResponse>() {
                        @Override
                        public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                            if (response.body() != null) {
                                Log.d("aaaa", "获得数据: " + response.body());
                                callback.onResult(response.body().users, null, FIRST_PAGE + 1);
                            }
                        }

                        @Override
                        public void onFailure(Call<UserResponse> call, Throwable t) {

                        }
                    });
        }

        @Override
        public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, User> callback) {
            //暂时用不到，什么都不用做
        }

        @Override
        public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, User> callback) {
            Log.d("aaaa", "下一页 key: " + params.key);
            RetrofitClient.get().buildApi(Api.BASE_URL2)
                    .getUsers(params.key, PageConfig.PER_PAGE_SIZE, SITE)
                    .enqueue(new Callback<UserResponse>() {
                        @Override
                        public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
                            if (response.body() != null) {
                                Log.d("aaaa", "下一页获得数据: " + response.body());
                                Integer nextKey = response.body().hasMore ? params.key + 1 : null;
                                callback.onResult(response.body().users, nextKey);
                            }
                        }

                        @Override
                        public void onFailure(Call<UserResponse> call, Throwable t) {

                        }
                    });
        }
    }

}
